﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.Manage.SqlServerDAL;


public partial class SysManage_UserManage_User_Group_Edit : System.Web.UI.Page
{
    UserManage MyUM = new UserManage();
    RoleManage MyRM = new RoleManage();
    public string UserRole = "", sDbRoleCode = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(SysManage_UserManage_User_Group_Edit));
        this.RoleName.Attributes.Add("onchange", "matchStationId(this.value)");

        UserRole = (string)Session["Baimasoft_Role_Code"];
        if (!Page.IsPostBack)
        {
            HOrgIDCol.Value = AspNetHelper.GetRightOrgIds();   //初始化树的数据权限


            if (ViewState["VOrgType"] == null || ViewState["VOrgType"].ToString().CompareTo("") == 0)
            {  //已被使用
                ViewState["VOrgType"] = "0";
            }

            try
            {
                ViewState["VOrgType"] = Request.QueryString["OrgType"].ToString();
                HOrgType.Value = ViewState["VOrgType"].ToString().Trim();
            }
            catch
            {
            }
            if (ViewState["VDbRoleCode"] == null || ViewState["VDbRoleCode"].ToString().CompareTo("") == 0)
            {
                ViewState["VDbRoleCode"] = "";
            }
            try
            {
                sDbRoleCode = Request.QueryString["DbRoleCode"].ToString();
            }
            catch
            {
                sDbRoleCode = "";
            }
            RoleBind(UserRole);
            DbRoleBind(sDbRoleCode);
            GroupDropBind();
        }
    }

    public void GroupDropBind()
    {
        DropGroupID.DataSource = MyUM .GetUserGroupInfo ();
        DropGroupID.DataBind();
        DropGroupID.Items.Insert(0, "顶级组织");
        DropGroupID.Items[0].Value = "0";
    }

    [AjaxPro.AjaxMethod()]
    public static string[] getMatchStation(string sStation)
    {
        DataTable  Mydt = (new Baimasoft.Manage.SqlServerDAL.UserManage()).GetUserGroupInfo(sStation);
        if (Mydt.Rows .Count != 0)
        {
            string[] result = new string[Mydt.Rows.Count];
            for (int i = 0; i < Mydt.Rows.Count; i++)
                result[i] = Convert.ToString(Mydt.Rows[i][0]) + "|" + Convert.ToString(Mydt.Rows[i][1]);
            return result;
        }
        else
        {
            return null;
        }
    }

    //默认数据绑定
    public void RoleBind(string sUserRole)
    {
        RoleName.DataSource = MyRM .GetMyCtrlRole (sUserRole, "1", "");
        RoleName.DataBind();
        RoleName.Items.Insert(0, "选择角色");
        RoleName.Items[0].Value = "0";
    }

    public void DbRoleBind(string sDbRoleCode)
    {
        DbRoleName.DataSource = MyUM .GetUserGroupInfo (sDbRoleCode);
        DbRoleName.DataBind();
    }

    protected void GrantBtn1_ServerClick(object sender, EventArgs e)
    {
        string sGroupID = GroupID.Text.ToString().Trim();
        string sHGroupID = HGroupID.Value.ToString().Trim();
        string sGroupName = GroupName.Text.ToString().Trim();
        string sParent = DropGroupID.SelectedValue.ToString().Trim();
        
        string sRoleCode = RoleName.SelectedValue.ToString().Trim();  //功能角色
        string sDbRoleCode = HAjaxDropID.Value.ToString().Trim();     //数据角色

        string sUserIDCol = HChoiceID_2.Value.ToString().Trim();
        string sUserNameCol = ChoiceName_2.Text.ToString().Trim();
        string sOrder = GOrder.Text.ToString().Trim();
        string sCreator = (string )Session["Baimasoft_Role_Name"];
        string sEffective = Effective.SelectedValue.ToString().Trim();
        string sEditType = hidEditType.Value.ToString().Trim();
        string sAutoID = hAutoID.Value.ToString().Trim();

        if (sGroupID == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户组编码不能为空!');</script>");
            return;
        }
        if (sGroupName == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('用户组名称不能为空!');</script>");
            return;
        }

        //if (sRoleCode == "0")
        //{
        //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择功能角色!');</script>");
        //    return;
        //}

        if (sEditType == "1") // 新建
        {
            int back = MyUM .InsertUserGroupInfo (sGroupID, sGroupName, sParent, sRoleCode, sDbRoleCode, sUserIDCol, sUserNameCol, sOrder, sCreator, sEffective);
            if (back == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('新建失败!');</script>");
                return;
            }
        }
        else
        {
            int back = MyUM.UpdateUserGroupInfo (sGroupID, sHGroupID, sGroupName, sParent, sRoleCode, sDbRoleCode, sUserIDCol, sUserNameCol, sOrder, sCreator, sEffective, sAutoID);
            if (back == 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");
                return;
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                return;
            }
        }

    }
}
